<?php
class CMyHtml extends CHtml
{
	public static function clientChange($event,&$htmlOptions)
	{
		return parent::clientChange($event,&$htmlOptions);
	}
	
	public static function checkBoxGroupList($name,$select,$data,$htmlOptions=array())	
	{
		
		if(substr($name,-2)!=='[]')
			$name.='[]';		
		
		$content='<ul>';
		
		if (is_array($data))
		{
			foreach ($data as $key=>$value)
			{
				if(is_array($value))
				{
					$content.='<li class="cbgl">'.CHtml::checkBox('',false,array()).CHtml::encode($key);
					$content.=CMyHtml::checkBoxGroupList($name,$select,$value,$htmlOptions);
					$content.='</li>';
				}
				else
				{
					$checked=!is_array($select) && !strcmp($key,$select) || is_array($select) && in_array($key,$select);
					//$checkAll=$checkAll && $checked;
					$baseID=sha1(uniqid());
					$htmlOptions['value']=$key;
					$htmlOptions['id']=$baseID;
					$content.='<li class="cbgli">'.CHtml::checkBox($name,$checked,$htmlOptions).CHtml::encode($value).'</li>';
				}
			}
		}	
		
		$content.='</ul>';
		
		$js=<<<EOD
jQuery("li.cbgl").each(function(index){
	var self=$(this);
	self.children('li.cbgl input:checkbox').each(function(index){
		$(this).click(function() {
			self.find('ul input:checkbox').attr('checked', this.checked);
		});
		$(this).attr('checked', !self.find("li.cbgli > input:not(:checked)").length);
	});
});
jQuery("li input:checkbox").click(function() {
	$(this).parents('li.cbgl').each(function(index){
		var p=$(this);
  	p.children('li.cbgl input:checkbox').each(function(index){ 
			$(this).attr('checked', !p.find("li.cbgli > input:not(:checked)").length);
		});
	});
});
EOD;
		
		$cs=Yii::app()->getClientScript();
		$cs->registerCoreScript('jquery');
		$cs->registerScript('bcad01a86f1a11e0a8e521c474f88096',$js);
		
		return $content;
	}
	
	public static function activeCheckBoxGroupList($model,$attribute,$data,$htmlOptions=array())	
	{
		self::resolveNameID($model,$attribute,$htmlOptions);
		$selection=self::resolveValue($model,$attribute);
		if($model->hasErrors($attribute))
			self::addErrorCss($htmlOptions);
		$name=$htmlOptions['name'];
		unset($htmlOptions['name']);
		
		$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
		return self::hiddenField($name,'',$hiddenOptions)
			. self::checkBoxGroupList($name,$selection,$data,$htmlOptions);
	}	
	
	
}